<%@page import="org.vectrics.cv.SkillType"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<%@ taglib uri="/WEB-INF/vectrics.tld" prefix="vectrics"%>

<%@ page import="org.vectrics.hibernate.ThreadProperties"%>
<%@ page import="org.vectrics.domain.Domain"%>
<%@ page import="java.util.Collection"%>
<%@ page import="java.util.List"%>
<%@ page import="org.vectrics.recruiter.RecruitServiceLocator"%>

<%@page import="org.vectrics.cv.Skill"%>
<%@page import="org.vectrics.cv.Level"%>
<%@page import="org.vectrics.provider.CoreServiceLocator"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.collections.Predicate"%>
<%@page import="org.apache.commons.collections.CollectionUtils"%>
<%@page import="org.vectrics.cv.PersonSkill"%>
<%@page import="org.vectrics.user.Person"%>
<%@page import="org.vectrics.cv.ExtraCertificate"%>
<%@page import="java.util.ArrayList"%>


<%@page import="org.vectrics.recruiter.struts.tag.cv.SkillsViewTag"%>
<%@page import="org.vectrics.cv.ListableSkill"%>
<%@page import="java.util.SortedMap"%>
<%@page import="java.util.SortedSet"%>
<%@page import="java.util.TreeSet"%>
<%@page import="java.util.Set"%><script language="JavaScript"
	type="text/javascript"
	src="/hrs-intranet/js/dynamicTree/DynamicTree.js"></script>
<script language="JavaScript" type="text/javascript"
	src="/hrs-intranet/js/dynamicTree/ie5.js"></script>
<link rel="stylesheet" type="text/css"
	href="/hrs-intranet/js/dynamicTree/DynamicTree.css" />

<%
    String userId = request.getParameter("personId");
    String contextPath = request.getContextPath();
    String userName = ThreadProperties.getPerson().getUser().getUserName();
    String password = ThreadProperties.getPerson().getUser().getPassword();
    Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(userId));

    // Recupero la lista de todos los skills padres
    Collection<Skill> parentSkillsAux = CoreServiceLocator.getPersonService().findAllParentSkill();
    Set<Skill> parentSkills = new TreeSet<Skill>(parentSkillsAux);

    // Recupero los levels
    Collection<Level> levels = CoreServiceLocator.getPersonService().findAllLevels();

    // Lista de todos los skills asociados a la persona
    Collection<PersonSkill> personSkills = person.getSkills();
    Skill parent = null;

    for (Skill s : parentSkillsAux) {
        if (s.getName().equals("Certificaciones de Microsoft")) {
            parent = s;
            break;
        }
    }

    if (parent != null) {
        for (ExtraCertificate ec : person.getCertificates()) {
            Skill sk = new Skill();
            sk.setId((-1) * ec.getId());
            sk.setName(ec.getName());
            sk.setParentSkill(parent);
            sk.setType(SkillType.EXTRA_CERTIFICATE);
            parent.getChildSkills().add(sk);

            PersonSkill psk = new PersonSkill();
            psk.setSkill(sk);
            psk.setId(sk.getId());
            psk.setPerson(person);

            personSkills.add(psk);
        }
    }

    if (personSkills == null) {
        personSkills = new ArrayList<PersonSkill>();
    }

    //seteo la request 
    request.setAttribute("parentSkills", parentSkills);
    request.setAttribute("levels", levels);

    String formAction = "/admin/user/resume/saveSkills.do?personId=" + userId;
%>

<table width="100%">
	<tr>
		<td valign="top"><jsp:useBean id="skillForm"
				class="org.vectrics.recruiter.struts.cv.SkillForm" scope="request" />

			<vectrics:errors debug="false" /> <html:form
				action="<%=formAction%>" method="POST">
				<table width="500">
					<tr>
						<td valign="middle">
							<div class="DynamicTree">
								<div class="wrap" id="tree">

									<%
									    // recorremos los skills padres - SKP1
									        // Los skill se cargan solo en 3 niveles como maximo.
									        for (Skill parentSkill : parentSkills) {

									            if ((parentSkill.getChildSkills() != null) && !parentSkill.getChildSkills().isEmpty()) {
									%>
									<div class="folder">
										<input type="checkbox" name="padre" value=""
											disabled="disabled" /><%=parentSkill.getName()%>

										<%
										    // recorremos los skills hijos - SKC1

										                Collection<Skill> skillsSubCategoriaAux = parentSkill.getChildSkills();
										                Set<Skill> skillsSubCategoria = new TreeSet<Skill>(skillsSubCategoriaAux);
										                for (Skill skill : skillsSubCategoria) {
										                    // si no tiene hijos lo dibujo
										                    if ((skill.getChildSkills() == null) || skill.getChildSkills().isEmpty()) {

										                        // buscamos si el skill hijo lo tiene la persona para poder checkearlo o no
										                        final Long idSkill = skill.getId();
										                        Predicate preSkill = new Predicate() {
										                            public boolean evaluate(Object objeto) {
										                                PersonSkill skillPre = (PersonSkill) objeto;
										                                return (skillPre.getSkill().getId().equals(idSkill));
										                            }
										                        };

										                        PersonSkill skillEncontrado = (PersonSkill) CollectionUtils
										                                .find(personSkills, preSkill);
										%>
										<%
										    if (skillEncontrado != null) {
										                            if (skill.getType() == SkillType.LEVELED_TYPE) {
										%>
										<div class="doc">
											<input type="checkbox"
												name="skill_<%=skillEncontrado.getSkill().getId()%>"
												value="<%=skill.getId()%>" checked="checked" /><%=skill.getName()%>
											<input type="hidden" id="l_<%=skill.getId()%>"
												value="<%=skillEncontrado.getLevel().getId()%>" />
										</div>
										<%
										    } else {
										%>

										<div class="doc boolean">
											<input type="checkbox"
												name="skill_<%=skillEncontrado.getSkill().getId()%>"
												value="<%=skill.getId()%>" checked="checked" /><%=skill.getName()%>
										</div>
										<%
										    }
										                        } else {

										                            if (skill.getType() == SkillType.LEVELED_TYPE) {
										%>
										<div class="doc">
											<input type="checkbox" name="skill_<%=skill.getId()%>"
												value="<%=skill.getId()%>" /><%=skill.getName()%>
											<input type="hidden" id="l_<%=skill.getId()%>" value="0" />
										</div>

										<%
										    } else {
										%>
										<div class="doc boolean">
											<input type="checkbox" name="skill_<%=skill.getId()%>"
												value="<%=skill.getId()%>" /><%=skill.getName()%>
										</div>
										<%
										    }
										                        }
										                    } else {
										                        //Si tiene hijos lo muestro y muestro sus hijos
										%>
										<div class="folder">
											<input type="checkbox" name="padre" value=""
												disabled="disabled" /><%=skill.getName()%>
											<%
											    // recorremos los skills hijos - SKC1

											                        Collection<Skill> skillsAux = skill.getChildSkills();
											                        Set<Skill> skills = new TreeSet<Skill>(skillsAux);
											                        for (Skill child : skills) {
											                            // buscamos si el skill hijo lo tiene la persona para poder checkearlo o no
											                            final Long idChild = child.getId();
											                            Predicate preChild = new Predicate() {
											                                public boolean evaluate(Object objeto) {
											                                    PersonSkill skillPre = (PersonSkill) objeto;
											                                    return (skillPre.getSkill().getId().equals(idChild));
											                                }
											                            };

											                            PersonSkill skillEncontrado = (PersonSkill) CollectionUtils.find(personSkills,
											                                    preChild);
											%>
											<div class="doc">
												<%
												    if (skillEncontrado != null) {
												%>
												<input type="checkbox"
													name="skill_<%=skillEncontrado.getSkill().getId()%>"
													value="<%=child.getId()%>" checked="checked" /><%=child.getName()%>
												<input type="hidden" id="l_<%=child.getId()%>"
													value="<%=skillEncontrado.getLevel().getId()%>" />
												<%
												    } else {
												%>
												<input type="checkbox" name="skill_<%=child.getId()%>"
													value="<%=child.getId()%>" /><%=child.getName()%>
												<input type="hidden" id="l_<%=child.getId()%>" value="0" />
												<%
												    }
												%>
											</div>
											<%
											    }
											%>
										</div>
										<%
										    }
										                } // END SKC1
										%>
									</div>
									<%
									    } // END IF
									        } // END SKP1
									%>
								</div>
							</div></td>
					</tr>
					<tr>
						<td valign="top" align="left"><vectrics:formButton
								name="submit" code="button.ok" phraseCode="button.ok" /> &nbsp;
							<vectrics:formButton name="cancel" code="button.cancel"
								phraseCode="button.cancel" />
						</td>
					</tr>
				</table>
			</html:form>
			<div style="display: none;" id="divLevel">
				<select name="level" id="level">
					<%
					    // recorremos y armamos los options del combo level
					    for (Level level : levels) {
					%>
					<option value="<%=level.getId()%>"><%=level.getName()%></option>
					<%
					    }
					%>
				</select>
			</div> <script type="text/javascript">
				var tree = new DynamicTree("tree");
				tree.init();
			</script></td>
		<td valign="top" align="right">
			<table cellpadding="0" cellspacing="0" width="100%">
				<tr>
					<td>
<%-- 						<html:link action="/hrs2/EditExtraCertificatesAction_editExtraCertificates.action">certificados</html:link> --%>
						<form
							action="/hrs-intranet/hrs2/EditExtraCertificatesAction_editExtraCertificates.action"
							method="POST">
							<input type="hidden" name="personId"
								value="<%=new Long(userId)%>"> <input type="hidden"
								name="userId" value="<%=new Long(userId)%>">
							<vectrics:formButton code="button.param"
								phraseCode="button.person.extraCertificates.edit"
								elementId="person.extraCertificates.edit.btn" value="submit"
								name="submit" permission="view_employees_items" />
						</form>
						</td>
				</tr>
			</table></td>
	</tr>
</table>